package com.huihui.utils;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.huihui.R;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;


/**
 * Created by Administrator on 2018/9/2.
 */

public class ShareUtils {
    private static final int THUMB_SIZE = 150;
    private static Activity mContext;
    private static IWXAPI api;

    public static void shareSelect(Activity context, String url,int type,String title,String content) {
        mContext = context;
        api = WXAPIFactory.createWXAPI(context, Contants.WEIXIN_APPID, false);
        if (type == 0) {
            WXWebpageObject webpage = new WXWebpageObject();
            webpage.webpageUrl = url;
            WXMediaMessage msg = new WXMediaMessage(webpage);
            msg.title = title ;
            msg.description = content;
                        Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), R.mipmap.logo);
                        Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, THUMB_SIZE, THUMB_SIZE, true);
                        bmp.recycle();
                        msg.thumbData = Util.bmpToByteArray(thumbBmp, true);
            SendMessageToWX.Req req = new SendMessageToWX.Req();
            req.transaction = buildTransaction("webpage");
            req.message = msg;
            req.scene = SendMessageToWX.Req.WXSceneSession;
            api.sendReq(req);
        } else {
            //分享到朋友圈
            WXWebpageObject webpage1 = new WXWebpageObject();
            webpage1.webpageUrl = url;
            WXMediaMessage msg1 = new WXMediaMessage(webpage1);
            msg1.title = "好价不用等";
            msg1.description = "测试";
//                        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.send_music_thumb);
//                        Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, THUMB_SIZE, THUMB_SIZE, true);
//                        bmp.recycle();
//                        msg.thumbData = Util.bmpToByteArray(thumbBmp, true);
            SendMessageToWX.Req req1 = new SendMessageToWX.Req();
            req1.transaction = buildTransaction("webpage");
            req1.message = msg1;
            req1.scene = SendMessageToWX.Req.WXSceneTimeline;
            api.sendReq(req1);
        }
    }

    private static String buildTransaction(final String type) {
        return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
    }

}
